// pages/lo/lo.js
import request from '../../utils/request.js'
 //没有剪切过的排行榜数据,即全部
let allsonglists = [];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 轮播图数据
    swiperList: [],
    //推荐歌单
    toplist:[],
    // 排行榜数据
    musicList: [],
    //排行榜歌曲
    // songlist: [],
   
   
    currentPage:0,//排行榜当前页
     topmuisclist:[],//获取排行榜列数数据
  },

  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: async function (options) {

    // 判断用户是否登录
    let userInfo = wx.getStorageSync('userInfo');
    if (!userInfo) {
      wx.showToast({
        title: '请先登录',
        icon: 'none',
        success: () => {
          // 跳转至登录界面
          wx.reLaunch({
            url: '/pages/login/login'
          })
        }
      })
      return;
    }

    let that = this;
  await request('/banner', { type: 1 })
   .then(res => {
     that.setData({
     swiperList : res.banners
     })
    });
    // 获取排行榜列表
  await request('/personalized')
      .then(res => {
       console.log(res)
        that.setData({
          toplist: res.result
        })
        // console.log(this.data.toplist)
      });
    //排行榜
    this.getmusiciplaylist()

     
    
    
   
   
  },

    //排行榜
 async getmusiciplaylist(){
    // console.log("aaaa")
    let { topmuisclist} = this.data
    // let topmuisclist = []
 let res = await request('/toplist')
     
        topmuisclist = res.list.slice(0, 5)
        this.setData({
          topmuisclist
        })
        console.log(topmuisclist)
         this.getplaylistdetail(topmuisclist)
 
  },
  //获取音乐列表
 async getplaylistdetail(topmuisclist){
    //  console.log(topmuisclist)
  let i = 0;
    // let songid = 0;
    let songlist = {};
    let musicList = [];
    
    while (i < 5) {
      //保留歌曲id
      //  console.log(that.data.musicList[i].id)
      // songid = topmuisclist[i].id
    console.log(i)
     await request('/playlist/detail', { id: topmuisclist[i].id })
        .then(res => {
          console.log(topmuisclist[i].id)
          console.log(res)
          console.log(res.playlist.tracks.slice(0, 3))
      
          allsonglists.push(res.playlist.tracks)
          console.log(allsonglists)
          songlist = { name: res.playlist.name, id: res.playlist.id, tracks: res.playlist.tracks.slice(0, 3) }
          i++
          musicList.push(songlist)
          // console.log(musicList)
          this.setData({
            musicList,
      
          })
       

        })
     
    }
  },
  //跳转到歌单详情页面
 async toplaylistdetail(e){
    //获取歌单id
    let playlistid = e.currentTarget.dataset.playlistid
   wx.navigateTo({
      url: '/pages/playlistdetail/playlistdetail?id=' + playlistid,
    })
  },
//跳转到reommendsongs页面
  torecommendsongs(){
    wx.navigateTo({
      url: '/pages/recommendsongs/recommendsongs',
    })
  },

  //跳转到播放音乐
 async playmusic(event) {
    let { songid, index } = event.currentTarget.dataset;
    let { currentPage } = this.data
    wx.setStorageSync('playingMusicList', allsonglists[currentPage]);
    wx.navigateTo({
      url: '/pages/songdetail/songdetail?songid=' + songid,
      // success: (res) => {
      //   // 通过eventChannel向被打开页面传送数据,这个data名字是你自己取的任意,在目标页面中有个参数接收就可以
      //   res.eventChannel.emit('parentPageEmit', { data: index });

      // },

    })
  },
  // 获取排行榜当前所处的页数
  getCurrentPage(e) {
    let { currentPage} = this.data
    currentPage = e.detail.current
    this.setData({
      currentPage
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})